package com.dongnam.quanly.common.controller;

import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.dao.ChiTietHoaDonDao;
import com.dongnam.quanly.common.dao.GenericDao;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.ChiTietDonHang;
import com.dongnam.quanly.common.model.ChiTietHoaDon;
import com.dongnam.quanly.common.model.compkey.ChiTietHoaDonId;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class ChiTietHoaDonController extends GenericController<ChiTietHoaDon, ChiTietHoaDonId> {

	private static final Logger logger = LoggerFactory.getLogger(ChiTietHoaDonController.class);

	public ChiTietHoaDonController() {
		super(new ChiTietHoaDonDao());
		// TODO Auto-generated constructor stub
	}

	@Override
	public ChiTietHoaDonDao getDao() {
		return (ChiTietHoaDonDao) dao;
	}

	public List<Object[]> getListCthd(String sodonhang) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<Object[]> list = getDao().getChiTietHoaDon(sodonhang);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

	public List<ChiTietHoaDon> getListCt(String hoadon) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			List<ChiTietHoaDon> list = getDao().getListCthd(hoadon);
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

	public boolean checkCthd(String hoadon, String maVthh) {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();
		try {
			boolean check = getDao().checkChiTiet(hoadon, maVthh);

			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return check;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}
}
